extends Control
class_name Setting

const MENU := "res://Scenes/Menu/main_menu.tscn"
const Transitioner := preload("res://Scenes/UI/TransitionScene.tscn")

var rand_rotate: float
var is_in_game: bool = true
var is_retrun_pressed: bool

@onready var music_slider: HSlider = %MusicSlider
@onready var sfx_slider: HSlider = %SFXSlider
@onready var _720p: Button = %"720P"
@onready var _1080p: Button = %"1080P"
@onready var full_screen: Button = %FullScreen
@onready var return_button: Button = %Return

func _ready() -> void:
	music_slider.value_changed.connect(_on_music_slider_changed)
	sfx_slider.value_changed.connect(_on_sfx_slider_changed)
	_720p.pressed.connect(_on_720p_pressed)
	_1080p.pressed.connect(_on_1080p_pressed)
	full_screen.pressed.connect(on_fullscreen_pressed)
	return_button.pressed.connect(func():
		is_retrun_pressed = true
		exit()
		)
	# 读取设置
	SaveManager.load_set()
	var setting = Global.current_setting
	if setting.has("BGM"):
		music_slider.value = setting["BGM"]
	if setting.has("SFX"):
		sfx_slider.value = setting["SFX"]
	if setting.has("SIZE"):
		var win_size = setting["SIZE"]
		if win_size == Vector2(1280, 720):
			_720p.button_pressed = true
		elif win_size == Vector2(1920, 1080):
			_1080p.button_pressed = true
		else:
			full_screen.button_pressed = true
		# 设置分辨率
		if win_size != Vector2.ZERO:
			DisplayServer.window_set_size(Vector2i(win_size))
		else:
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)


func enter() -> void:
	# 读取保存的设置并应用到UI
	var setting = Global.current_setting
	if setting.has("BGM"):
		music_slider.value = setting["BGM"]
	if setting.has("SFX"):
		sfx_slider.value = setting["SFX"]
	if setting.has("SIZE"):
		var win_size = setting["SIZE"]
		if win_size == Vector2(1280, 720):
			_720p.button_pressed = true
		elif win_size == Vector2(1920, 1080):
			_1080p.button_pressed = true
		else:
			full_screen.button_pressed = true
		# 设置分辨率
		DisplayServer.window_set_size(Vector2i(win_size))
	visible = true
	MusicPlayer.stop_all_sfx()
	get_tree().paused = true
	rand_rotate = deg_to_rad(randf_range(-15,15))
	var tween = create_tween()
	tween.set_parallel()
	tween.set_ignore_time_scale()
	tween.tween_property(self,"modulate:a",1,0.5).from(0)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)

func _on_720p_pressed():
	var mode = DisplayServer.window_get_mode()
	if mode == DisplayServer.WINDOW_MODE_FULLSCREEN:
		DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false)
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	DisplayServer.window_set_size(Vector2i(1280, 720))
	# 保存设置
	Global.current_setting["SIZE"] = Vector2(1280, 720)
	_save_setting()

func _on_1080p_pressed():
	var mode = DisplayServer.window_get_mode()
	if mode == DisplayServer.WINDOW_MODE_FULLSCREEN:
		DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false)
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	DisplayServer.window_set_size(Vector2i(1920, 1080))
	# 保存设置
	Global.current_setting["SIZE"] = Vector2(1920, 1080)
	_save_setting()

func _on_music_slider_changed(value: float) -> void:
	# 线性转分贝，Godot推荐范围[-80,0]，滑块0~1
	AudioServer.set_bus_volume_db(AudioServer.get_bus_index("BGM"), linear_to_db(value))
	Global.current_setting["BGM"] = value
	_save_setting()

func _on_sfx_slider_changed(value: float) -> void:
	AudioServer.set_bus_volume_db(AudioServer.get_bus_index("SFX"), linear_to_db(value))
	Global.current_setting["SFX"] = value
	_save_setting()
func _save_setting():
	# 调用SaveManager保存设置
	
	SaveManager.save_set()

func on_fullscreen_pressed():
	var mode = DisplayServer.window_get_mode()
	if mode != DisplayServer.WINDOW_MODE_FULLSCREEN:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	else:
		DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false)
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	Global.current_setting["SIZE"] = Vector2.ZERO
	_save_setting()
	

func exit() -> void:
	var tween = create_tween()
	tween.set_parallel()
	tween.set_ignore_time_scale()
	tween.tween_property(self,"modulate:a",0,0.5).from(1)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.chain().tween_callback(func(): 
		visible = false
		get_tree().paused = false
		)
	if is_in_game && is_retrun_pressed:
		var transition :Transition = Transitioner.instantiate()
		transition.next_scene_path = MENU
		transition.is_show_prograss = true
		add_child(transition)
		
func _shortcut_input(event: InputEvent) -> void:
	if event.is_action_pressed("set") && is_in_game:
		if visible:
			exit()
		else:
			enter()
